<?php

namespace app\admin\controller\site;

use app\admin\controller\Base;
use app\admin\controller\Qiniu;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Product extends Base
{

    public function index()
    {
        return view();
    }

    public function add()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = new \app\admin\validate\Product();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                $post['c_time'] = time();
                $post['info'] = $this->request->param('info', '', null);
                $info = Db::name('product')->where(['id' => $post['id']])->find();
                if (isset($post['id']) && !empty($post['id'])) {
                    preg_match_all('/(?=\/qiniu).+?(?=")/', $post['info'], $files_new);
                    $qiniu = new Qiniu();
                    preg_match_all('/(?=\/qiniu).+?(?=")/', $info['info'], $files);
                    foreach ($files[0] as $item) {
                        if (in_array($item, $files_new[0])) {
                            //不操作
                        } else {
                            $qiniu->delqiniu($item);
                        }
                    }
                    if ($post['file_path'] != $info['file_path']) {
                        $qiniu->delqiniu($info['file_path']);
                    }
                    DB::name('product')->update($post);
                } else {
                    DB::name('product')->insert($post);
                }
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            if (isset($post['id']) && !empty($post['id'])) {
                $info = DB::name('product')->where('id', $post['id'])->find();
                $this->assign('info', $info);
            }
            return view();
        }
    }

    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }

            Db::startTrans();
            try {
                $info = Db::name('product')->where(['id' => $post['id']])->find();
                $qiniu = new Qiniu();
                preg_match_all('/(?=\/qiniu).+?(?=")/', $info['info'], $files);
               //详情里面的图片
                foreach ($files[0] as $item) {
                    $qiniu->delqiniu($item);
                }
                $qiniu->delqiniu($info['file_path']);
                Db::name('product')->where(['id' => $post['id']])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $data['count'] = Db::name('product')
            ->where($map)
            ->count();
        $data['data'] = Db::name('product')
            ->where($map)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
        foreach ($data['data'] as &$info) {
            $info['c_time'] = date('Y-m-d', $info['c_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


}